﻿using System.Threading.Tasks;

namespace Sidvall.Office
{
    public class ExcelManager
    {
        #region Public Members

        public Sidvall.Office.IExcel Excel { get; set; }
        #region StorageManager

        private Sidvall.IO.StorageManager _StorageManager;
        public Sidvall.IO.StorageManager StorageManager
        {
            get
            {
                if (_StorageManager != null)
                    return _StorageManager;
                return Sidvall.SystemContext.Current.SystemStorageManager;
            }
            set
            {
                _StorageManager = value;
            }
        }

        #endregion

        #region CreateSolidFill

        public object CreateSolidFill(Color color)
        {
            return this.Excel.CreateSolidFill(color);
        }

        #endregion
        #region CreateWorkbookColorInfo

        public object CreateWorkbookColorInfo(Color color, double tint)
        {
            return this.Excel.CreateWorkbookColorInfo(color, tint);
        }

        #endregion
        #region LoadWorkbookAsync

        public async Task<IWorkbook> LoadWorkbookAsync(System.IO.Stream stream)
        {
            return await this.Excel.LoadWorkbookAsync(stream).ConfigureAwait(false);
        }
        public async Task<IWorkbook> LoadWorkbookAsync(string fileName)
        {
            using (var stream = await this.StorageManager.OpenFileAsync(fileName, IO.FileMode.Open, IO.FileAccess.Read).ConfigureAwait(false))
            {
                return await LoadWorkbookAsync(stream).ConfigureAwait(false);
            }
        }

        #endregion
        #region NewWorkbookAsync

        public async Task<IWorkbook> NewWorkbookAsync(WorkbookFormat format)
        {
            return await this.Excel.NewWorkbookAsync(format).ConfigureAwait(false);
        }

        #endregion

        #endregion
        #region Constructors

        public ExcelManager(Sidvall.Office.IExcel excel)
        {
            this.Excel = excel;
        }

        #endregion
    }
}
